home *** CD-ROM | disk | FTP | other *** search
/ Amiga CD-Sensation: Golden Games / Amiga CD-Sensation - Ausgabe 2 - Golden Games (1996)(GTI - Schatztruhe)(DE)[!].iso / Archive / Funny Stuff / styx.lha / styx / styx.c < prev    next >
C/C++ Source or Header  |  1995-09-18  |  8KB  |  408 lines

  1. /*
  2. ** styx.c
  3. **
  4. ** a crash indicator for Apollo 2030 users
  5. **
  6. ** Version: see below (version_str)
  7. **
  8. ** TODO:
  9. ** - Add Menu: About/Options../Quit
  10. ** - Add Options requester to set delay & smart flag
  11. ** - Add args & tooltypes
  12. */
  13.  
  14. /*
  15. ** amiga includes
  16. */
  17. #include <exec/types.h>
  18. #include <intuition/intuition.h>
  19. #include <intuition/intuitionbase.h>
  20. #include <dos/dos.h>
  21. #include <graphics/gfxbase.h>
  22.  
  23. #ifndef __MAXON__
  24.  
  25. /* 
  26. ** generic includes 
  27. */
  28. #include <clib/exec_protos.h>
  29. #include <clib/dos_protos.h>
  30. #include <clib/graphics_protos.h>
  31. #include <clib/intuition_protos.h>
  32.  
  33. #else
  34.  
  35. /* 
  36. ** Maxon/c includes & defs
  37. */
  38. #include <pragma/exec_lib.h>
  39. #include <pragma/dos_lib.h>
  40. #include <pragma/graphics_lib.h>
  41. #include <pragma/intuition_lib.h>
  42.  
  43. /* output window for wbmain() */
  44. #define WBWINNAME "CON:0/1/400/100/styx - Output/CLOSE/WAIT/AUTO"
  45. #include <wbstartup.h>
  46.  
  47. #endif /* __MAXON__ */
  48.  
  49. /*
  50. ** ansi includes
  51. */
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54.  
  55.  
  56.  
  57. #define MAX_STXP_NUM 1000  /* max. num of points */
  58.  
  59. #define SIZE_STEP (1<<2)   /* num. of pixels a stick steps */
  60. #define MASK_STEP ((ULONG)-(SIZE_STEP))
  61.  
  62. #define MIN_WIDTH    48
  63. #define MIN_HEIGHT   48
  64. #define MAX_WIDTH    -1
  65. #define MAX_HEIGHT   -1
  66.  
  67. /*
  68. ** global vars
  69. */
  70. struct IntuitionBase *IntuitionBase = NULL;
  71. struct GfxBase       *GfxBase = NULL;
  72. struct Window        *StyxWin = NULL;
  73.  
  74. ULONG width  = 100; /* default window inner size */
  75. ULONG height = 72;  
  76. ULONG ptnum  = 12;  /* number of styx points */
  77. ULONG delay  = 9;   /* 1/50 of second */
  78. ULONG smart  = 1;   /* 1: redraw ALL lines after last line cleared (slow) */
  79.                     /* 0: no redraw after last line erased (ugly&fast) */
  80.  
  81. ULONG old_width;    /* old window size */
  82. ULONG old_height;
  83.  
  84. /*
  85. ** structure for styx point
  86. */
  87. typedef struct styx_rec {
  88.  
  89.     LONG pos_x; /* position */
  90.     LONG pos_y;
  91.     BYTE dir_x;  /* direction */
  92.     BYTE dir_y;
  93.  
  94. } STXP;
  95.  
  96. STXP stxp1[ MAX_STXP_NUM ],
  97.      stxp2[ MAX_STXP_NUM ];
  98.  
  99. struct RastPort *rp = NULL;
  100. BYTE bx = 0; /* border size of window */
  101. BYTE by = 0;
  102.  
  103.  
  104. /*
  105. ** proto types
  106. */
  107. BOOL open_libs( VOID );
  108. VOID cleanup( VOID );
  109. VOID draw_styx( VOID );
  110. int get_rand( int max );
  111. VOID inc_stxp( STXP *stxp );
  112. VOID draw_stxpline( LONG num, BYTE pen );
  113.  
  114.  
  115. /*
  116. ** open_libs
  117. **
  118. ** open intuition & graphics library
  119. */
  120. BOOL open_libs( VOID )
  121. {
  122.     BOOL ok = TRUE;
  123.  
  124.     IntuitionBase = (struct IntuitionBase *)
  125.         OpenLibrary( "intuition.library", 37L );
  126.     if ( !IntuitionBase ) {
  127.  
  128.         fprintf( stderr, "Can't open \"Intuition.library\" v37\n" );
  129.         ok = FALSE;
  130.  
  131.     }
  132.     GfxBase = (struct GfxBase *)
  133.         OpenLibrary( "graphics.library", 37L );
  134.     if ( !GfxBase ) {
  135.  
  136.         fprintf( stderr, "Can't open \"graphics.library\" v37\n" );
  137.         ok = FALSE;
  138.  
  139.     }
  140.  
  141.     return( ok );
  142. }
  143.  
  144.  
  145. /*
  146. ** cleanup
  147. */
  148. VOID cleanup( VOID )
  149. {
  150.     if ( IntuitionBase)
  151.         CloseLibrary( (struct Library *) IntuitionBase );
  152.     if ( GfxBase)
  153.         CloseLibrary( (struct Library *) GfxBase );
  154.     if ( StyxWin )
  155.         CloseWindow( StyxWin );
  156. }
  157.  
  158.  
  159. /* get_rand */
  160. int get_rand( int max )
  161. {
  162.     int rnd;
  163.  
  164.     do {
  165.         rnd = rand() & 0xfff;
  166.     } while ( rnd > max );
  167.  
  168.     rnd = rnd & MASK_STEP;
  169.  
  170.     return ( rnd );
  171.  
  172.  
  173. }
  174.  
  175. /* inc_stxp */
  176. VOID inc_stxp( STXP *stxp )
  177. {
  178.  
  179.     /* flip directions if neccessary */
  180.     if ( (stxp->pos_x==0) || (stxp->pos_x==width) )
  181.         stxp->dir_x = (-stxp->dir_x);
  182.     if ( (stxp->pos_y==0) || (stxp->pos_y==height) )
  183.         stxp->dir_y = (-stxp->dir_y);
  184.  
  185.     /* update position */
  186.     stxp->pos_x += (stxp->dir_x*SIZE_STEP);
  187.     stxp->pos_y += (stxp->dir_y*SIZE_STEP);
  188.  
  189. #if 0
  190.     printf( "(%d/%d) [%d/%d]\n",
  191.              stxp->pos_x, stxp->pos_y, stxp->dir_x, stxp->dir_y );
  192. #endif
  193.  
  194. }
  195.  
  196. VOID draw_stxpline( LONG num, BYTE pen )
  197. {
  198.  
  199.     SetAPen( rp, pen );
  200.     Move( rp, bx+stxp1[num].pos_x, by+stxp1[num].pos_y );
  201.     Draw( rp, bx+stxp2[num].pos_x, by+stxp2[num].pos_y );
  202. }
  203.  
  204. /*
  205. ** init_styx
  206. */
  207. VOID init_styx( VOID )
  208. {
  209.     int rndx1, rndx2, rndy1, rndy2;    /* random start position */
  210.     LONG i;                            /* loop var */
  211.  
  212.     /* adjust width & height */
  213.     width  = width  & MASK_STEP;
  214.     height = height & MASK_STEP;
  215.     old_width = width;
  216.     old_height = height;
  217.  
  218.     /* compute random start position */
  219.     rndx1 = get_rand( width );
  220.     rndx2 = get_rand( width );
  221.     rndy1 = get_rand( height );
  222.     rndy2 = get_rand( height );
  223.  
  224.     /* init rastport vars */
  225.     rp = StyxWin->RPort;
  226.     bx = StyxWin->BorderLeft;
  227.     by = StyxWin->BorderTop;
  228.  
  229.     /* init styx points */
  230.     for ( i=0; i<ptnum; i++ ) {
  231.  
  232.         stxp1[i].pos_x = rndx1;
  233.         stxp1[i].pos_y = rndy1;
  234.         stxp1[i].dir_x = 1;
  235.         stxp1[i].dir_y = 1;
  236.         stxp2[i].pos_x = rndx2;
  237.         stxp2[i].pos_y = rndy2;
  238.         stxp2[i].dir_x =  1;
  239.         stxp2[i].dir_y = -1;
  240.  
  241.     }
  242.  
  243.  
  244. }
  245.  
  246. /*
  247. ** draw_styx
  248. */
  249. VOID draw_styx( VOID )
  250. {
  251.     LONG  i;       /* loop vars */
  252.     STXP *stxp;    /* styx-point */
  253.  
  254.     SetAPen( rp, 1 );
  255.  
  256.     /* erase last stxp-line*/
  257.     draw_stxpline( ptnum-1, 0 );
  258.  
  259.     /* erase last styx-position */
  260.     for ( i=ptnum-1; i>0; i-- ) {
  261.  
  262.         stxp1[i] = stxp1[i-1];
  263.         stxp2[i] = stxp2[i-1];
  264.  
  265.     }
  266.  
  267.     /* if smart, redraw all lines */
  268.     /* TODO: only lines that have been overwritten */
  269.     if ( smart )
  270.         for ( i=ptnum-1; i>0; i-- )
  271.             draw_stxpline( i, 1 );
  272.  
  273.     /* move first styx */
  274.     stxp = &(stxp1[0]);
  275.     inc_stxp( stxp );
  276.     stxp = &(stxp2[0]);
  277.     inc_stxp( stxp );
  278.  
  279.     /* draw new stxp-line*/
  280.     draw_stxpline( 0, 1 );
  281.  
  282. }
  283.  
  284. /*
  285. ** resize_styx
  286. */
  287. VOID refresh_styx( VOID )
  288. {
  289.     struct IntuiMessage *imsg;
  290.     BOOL                 sized = FALSE;
  291.     ULONG                i;
  292.  
  293.     /* compute new width & height */
  294.     width  = StyxWin->Width - StyxWin->BorderLeft - StyxWin->BorderRight -1;
  295.     height = StyxWin->Height - StyxWin->BorderTop - StyxWin->BorderBottom -1;
  296.     width  = width  & MASK_STEP;
  297.     height = height & MASK_STEP;
  298.  
  299. #if 0
  300.     printf( "refresh: (%d,%d) old:(%d,%d)\n",
  301.             width, height, old_width, old_height );
  302. #endif
  303.  
  304. #if 0
  305.     /* wait for IDCMP_CHANGEWINDOW */
  306.     WaitPort( StyxWin->UserPort );
  307.     while ( imsg = (struct IntuiMessage *) GetMsg( StyxWin->UserPort ) ) {
  308.  
  309.         if ( imsg->Class & IDCMP_CHANGEWINDOW )
  310.             sized = TRUE;
  311.         ReplyMsg( (struct Message *) imsg );
  312.  
  313.     }
  314. #endif
  315.  
  316.  
  317.     /* clear whole styx */
  318.     for ( i=0; i<ptnum; i++ )
  319.         draw_stxpline( i, 0 );
  320.  
  321.     /* strip styx points */
  322.     /* TODO: improve this */
  323.     if ( (width != old_width) || (height != old_height) )
  324.         init_styx();
  325.  
  326. }
  327.  
  328. /*
  329. ** open styx window
  330. */
  331. BOOL open_styx_window( void )
  332. {
  333.     BOOL ok = TRUE;
  334.  
  335.     struct TagItem SWTags[] = {
  336.         { WA_InnerWidth, (ULONG) 0 },
  337.         { WA_InnerHeight, (ULONG) 0 },
  338.         { WA_MinWidth, (ULONG) MIN_WIDTH },
  339.         { WA_MinHeight, (ULONG) MIN_HEIGHT },
  340.         { WA_MaxWidth, (ULONG) MAX_WIDTH },
  341.         { WA_MaxHeight, (ULONG) MAX_HEIGHT },
  342.         { WA_Left, (ULONG) 599 },
  343.         { WA_Title, (ULONG) "styx" },
  344.         { WA_IDCMP, IDCMP_CLOSEWINDOW | /*IDCMP_SIZEVERIFY | */
  345.                     IDCMP_REFRESHWINDOW },
  346.         { WA_Flags, WFLG_CLOSEGADGET | WFLG_DRAGBAR |
  347.                     WFLG_DEPTHGADGET | WFLG_SIZEGADGET |
  348.                     WFLG_SIZEBBOTTOM | WFLG_SIMPLE_REFRESH },
  349.         { TAG_DONE, NULL }
  350.     };
  351.  
  352. #if 0
  353.     printf( "STEP:  %d\nMASK  : %x\nwidth : %d\nheight: %d\n",
  354.             SIZE_STEP, MASK_STEP, width, height );
  355. #endif
  356.  
  357.     SWTags[0].ti_Data = (ULONG) width+1;
  358.     SWTags[1].ti_Data = (ULONG) height+1;
  359.  
  360.     StyxWin = OpenWindowTagList( NULL, SWTags );
  361.     if ( !StyxWin )
  362.         ok = FALSE;
  363.  
  364.     return( ok );
  365. }
  366.  
  367. VOID main( int argc, char *argv[] )
  368. {
  369.     static STRPTR version_str = "$VER: 1.0 styx (19.9.1995)";
  370.  
  371.     if ( open_libs() && open_styx_window() ) {
  372.  
  373.         BOOL quit = FALSE;
  374.         struct IntuiMessage *imsg;
  375.  
  376.         init_styx();
  377.  
  378.         do {
  379.  
  380.             /* draw the styx */
  381.             draw_styx();
  382.  
  383.             /* wait a bit */
  384.             Delay( delay );
  385.  
  386.             /* get message and check for close */
  387.             imsg = (struct IntuiMessage *) GetMsg( StyxWin->UserPort );
  388.             if ( imsg ) {
  389.  
  390.                 if ( imsg->Class & IDCMP_CLOSEWINDOW )
  391.                     quit = TRUE;
  392.  
  393.                 else if ( imsg->Class & IDCMP_REFRESHWINDOW )
  394.                     refresh_styx();
  395.  
  396.                 ReplyMsg( (struct Message *) imsg );
  397.  
  398.             }
  399.  
  400.         } while ( !quit );
  401.     }
  402.  
  403.     cleanup();
  404. }
  405.  
  406.  
  407.  
  408.